package com.purong.purong.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;


import com.purong.purong.R;
import com.purong.purong.adapter.ChoseAreaAdapter;
import com.purong.purong.bean.AreaBean;
import com.purong.purong.util.CommonUtil;

import java.util.ArrayList;


/**
 * Created by Administrator on 2016/8/4.
 */
public class ChoseAreaDialog extends Dialog{
    private Context mContext;
    private OnAreaChoseListener mListener;
    private ArrayList<AreaBean> mItemList =new ArrayList<>();
    private RecyclerView mRecycleView;
    private ChoseAreaAdapter mAdapter;
    public ChoseAreaDialog(Context context, ArrayList<AreaBean> itemList, OnAreaChoseListener listenr) {
        super(context, R.style.DialogTranBgDimEnabled);
        this.mContext=context;
        if (itemList!=null)
        this.mListener=listenr;
        this.mItemList =itemList;
        setCanceledOnTouchOutside(true);
        Window window = this.getWindow();
        window .setGravity(Gravity.LEFT | Gravity.TOP);
        WindowManager.LayoutParams lp = window.getAttributes();
        int[] location=new int[2];
//        v.getLocationOnScreen(location);
//        Log.e("zwj","x="+location[0]);
        lp.x = 0;
        lp.y = CommonUtil.dip2px(context,45);


        window .setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mListener!=null){
            mListener.onDismiss();
        }
    }

    @Override
    public void show() {
        super.show();
        if (mListener!=null){
            mListener.onShow();
        }
        if (mAdapter!=null){
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_chose_bank, null);
        setContentView(inflate);
        findViewById(R.id.main_lay).setLayoutParams(new FrameLayout.LayoutParams(CommonUtil.getScreenWidth(mContext), CommonUtil.getScreenHeight(mContext)-CommonUtil.getStatusBarHeight(mContext)-CommonUtil.dip2px(mContext,45)));
        findViewById(R.id.main_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChoseAreaDialog.this.dismiss();
            }
        });
        mRecycleView=(RecyclerView) findViewById(R.id.recycler_view);
        mAdapter=new ChoseAreaAdapter(mContext);
        mAdapter.setData(mItemList);
        mAdapter.setOnAreaListener(new ChoseAreaAdapter.OnAreaListener() {
            @Override
            public void onClick(int position) {
                for (int i = 0; i < mItemList.size(); i++) {
                    if (mItemList.get(i).getArea_id().equals(mItemList.get(position).getArea_id())){
                        mItemList.get(i).setCheck(true);
                    }else {
                        mItemList.get(i).setCheck(false);
                    }
                }
                if (mListener!=null){
                    mListener.onChose(position);
                }
                ChoseAreaDialog.this.dismiss();
            }
        });
        mRecycleView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL,false));
        mRecycleView.setAdapter(mAdapter);

    }

    public interface OnAreaChoseListener{
        void onChose(int position);
        void onShow();
        void onDismiss();
    }
}
